package com.jiesone.logger.distributecleaning.config;
/*
 *@ClassName CommonRediskey
 *@Description TODO
 *@Author tangsixiang@163.com
 *@Date 2022/1/18 14:13
 *@Version 1.0
 */

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class CommonRediskey {

    Logger logger=  LoggerFactory.getLogger(CommonRediskey.class);


  //   public static   Map<String,Object > accessSourceMap=new HashMap<>();
    /**
     * 存储收集源数据
     */
    public   final   static  String  accessSource="logger:accesssource:map";
    /**
     * 配置保存数据源信息
     */

   // public static   Map<String,Object> savaTarge=new HashMap<>();


    public final static  String  savaTarget="logger:savaTarget:codeMap";


    /**
     * 告警规则设置
     */
    public  final  static String  aleamRule="logger:alarm:ruleMap";

  //  public static  Map<String,String > aleamRuleMap=new HashMap<>();


    /**
     * 日志清洗规则链     1 智能
     */
    public  final  static  String   dataRuleSet="logger:dataRule:codeList:%s:type:%s";

    //public  final  static String   dataRuleDescMap="logger:dataRule:codeDesc:code:%s:ruleId:%s";




    //  日志加工规则配置信息判断Map
    public  final  static  String   machingRule="logger:machingRule:indexMap:type:1";


    //待处理的日志加工数据列表
    public  final  static  String   machingLogData="logger:machingRule:Data:list";
   /* public     Map<String,Object> getAccessSourceMap(){

        if(accessSourceMap.size()>=1){
            logger.debug("---------- 刷新配置日志收集源配置-----------------");
            // 查询y有效的配置数据源
            List<Map<String, Object>> dataSource = jdbcTemplate.queryForList("SELECT source_code sourceCode,source_host sourceHost ,data_code dataCode , source_format sourceFormat, source_data  sourceData  ,save_level saveLevel ,source_flag sourceFlag FROM logger_source where  source_flag=1 ");
      //      stringRedisTemplate.delete(CommonRediskey.accessSource);
            CommonRediskey.accessSourceMap.clear();
            Map<String,Map<String,Object>>   accessSourceMap=new HashMap<>();
            for (Map<String,Object> map:dataSource){
                accessSourceMap.put(map.get("sourceCode").toString(),map);
                logger.debug(JSON.toJSONString(map));
            }
            accessSourceMap.putAll(accessSourceMap);
      //      stringRedisTemplate.opsForHash().putAll(CommonRediskey.accessSource,accessSourceMap); //  刷新并存储Redis库中目标符合要求的


        }

        return accessSourceMap;
    }

    public     Object  getAccessSourceMap(String key){
        return getAccessSourceMap().get(key);
    }



    public    Map<String,Object>  getSavaTarge(){

         if(savaTarge.size()>=1){
             // 查询y有效的配置数据源
             List<Map<String, Object>> targetSource = jdbcTemplate.queryForList("SELECT  t.`target_id` targetId,t.target_index targetIndex  ,t.`source_id` sourceId,s.`source_code` sourceCode, t.`target_data` targetData  ,t.`target_type` targetType,t.`target_flag` targetFlag FROM `logger_target`  t LEFT JOIN `logger_source`  s ON t.`source_id`=s.`soruce_id` where t.target_flag=1  order by  target_flag desc   ");
             Map<String,Map<String, Object>>   savaTargetMap=new HashMap<>();
       //      stringRedisTemplate.delete(CommonRediskey.savaTargetMap);
             CommonRediskey.savaTarge.clear();
             for (Map<String,Object> map:targetSource){
                 savaTargetMap.put(map.get("sourceCode")+":"+map.get("targetType"),map);
                 logger.debug(JSON.toJSONString(map));
             }
             savaTarge.putAll(savaTargetMap);
             //    stringRedisTemplate.opsForHash().putAll(CommonRediskey.savaTargetMap,savaTargetMap);


         }


        return savaTarge;
    }
    public    Object  getSavaTarge(String key){

        return getSavaTarge().get(key);
    }


    *//***
     * 获取 源数据告警规则功能
     * @return
     *//*
    public     Map<String,String >  getaleamRuleMap(){


        if(aleamRuleMap.size()>=1){

            *//**
             *  设置日志告警规则加载到缓存库中方便快速查询
             *//*
            List<Map<String, Object>> alarmList=    jdbcTemplate.queryForList("SELECT * FROM `logger_alarm_rule` where alarm_flag=1  ORDER BY alarm_flag  DESC ");
       //     stringRedisTemplate.delete(CommonRediskey.aleamRule);
            Map<String,String >   alarmRule=new HashMap<>();
            for(Map<String, Object>  map:alarmList){
                alarmRule.put(map.get("source_code")+":"+map.get("alarm_type"),map.get("alarm_level").toString());
            }
        //    stringRedisTemplate.opsForHash().putAll(CommonRediskey.aleamRule,alarmRule);


            aleamRuleMap.putAll(alarmRule);
        }


        return aleamRuleMap;
    }

    public    String  getaleamRuleMap(String key){

        return getaleamRuleMap().get(key);
    }

*/

}
